﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.AssetsBonus.DAL.Bonus.Model;

namespace XQ.AssetsBonus.DAL.BonusManage
{
    public class BonusManageDAL
    {
        public string save(BonusBatch detail)
        {
            string result = "";
            using (DataContext db = new DataContext())
            {
                if (detail != null)
                {
                    var bonusBatches = db.BonusBatch.Where(b => b.ID == detail.ID);
                    int count = bonusBatches.Count();
                    if (count > 0)
                    {
                        db.Entry<BonusBatch>(detail).State = System.Data.Entity.EntityState.Modified;
                        if (db.SaveChanges() == 1) { result = "保存成功!"; }
                        else { result = "保存失败!"; }
                    }
                    else
                    {
                        db.BonusBatch.Add(detail);
                        if (db.SaveChanges() == 1)
                        {
                            result = "保存成功!";
                        }
                        else
                        {
                            result = "保存失败!";
                        }
                    }
                }
                else
                {
                    result = "保存失败";
                }
            }
            return result;
        }

        public string delete(List<int> item)
        {
            string result = "";
            using (DataContext db = new DataContext())
            {
                if (item.Count > 0)
                {
                    foreach (int i in item)
                    {
                        BonusBatch bonusBatch = db.BonusBatch.Where(b => b.ID == i).FirstOrDefault();
                        if (bonusBatch != null)
                        {
                            db.BonusBatch.Remove(bonusBatch);
                            if (db.SaveChanges() == 1)
                            {
                                result = "删除成功!";
                            }
                            else
                            {
                                result = "删除失败!";
                                break;
                            }
                        }
                        else
                        {
                            result = "删除失败!";
                            break;
                        }
                    }
                }
                else
                {
                    result = "删除失败!";
                }
            }
            return result;
        }

        public List<BonusBatch> Search(BonusSearchModel bonusSearchModel)
        {
            List<BonusBatch> list = new List<BonusBatch>();
            using (DataContext db = new DataContext())
            {
                list = db.BonusBatch.ToList();
                if (bonusSearchModel != null)
                {
                    if (!string.IsNullOrEmpty(bonusSearchModel.BonusName))
                    {
                        list = db.BonusBatch.Where(b => b.BonusName.Contains(bonusSearchModel.BonusName)).ToList();
                    }
                    if (bonusSearchModel.StartTime != null)
                    {
                        list = list.Where(l => l.StartTime >= bonusSearchModel.StartTime).ToList();
                    }
                    if (bonusSearchModel.EndTime != null)
                    {
                        list = list.Where(l => l.EndTime <= bonusSearchModel.EndTime).ToList();
                    }
                }
            }
            return list;
        }
        public BonusBatch GetBonusById(int id)
        {
            BonusBatch bonusBatch = new BonusBatch();
            using (DataContext db = new DataContext())
            {
                bonusBatch = db.BonusBatch.Where(b => b.ID == id).FirstOrDefault();
            }
            return bonusBatch;
        }
    }
}
